//
//  UIView+STTools_moreex.m
//  STRich
//
//  Created by stoneobs on 2017/7/7.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#define STRGB(v)  [UIColor colorWithRed:((float)((v & 0xFF0000) >> 16))/255.0 green:((float)((v & 0xFF00) >> 8))/255.0  blue:((float)(v & 0xFF))/255.0 alpha:1]
#define ST_STONEOBS_UI_FIRST_TEXT_COLOR       STRGB(0x333333)
#define ST_STONEOBS_UI_SECEND_TEXT_COLOR      STRGB(0x666666)
#define ST_STONEOBS_UI_THIRD_TEXT_COLOR       STRGB(0x999999)
#define ST_lineColor            STRGB(0xe0e4e5)

#import "UIView+STTools_moreex.h"

@implementation UIView (STTools_moreex)
- (void)stoneobs_fun_st_showAnimationWithType:(STAnimationType_stoneobs_enum)type{
    CATransition * stoneobs_temp_transion = [CATransition new];
    stoneobs_temp_transion.type = [self stringWithType:type];
    stoneobs_temp_transion.duration = 1;
    stoneobs_temp_transion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:stoneobs_temp_transion forKey:nil];
}
- (void)stoneobs_fun_st_showAnimationWithType:(STAnimationType_stoneobs_enum)type duration:(CFTimeInterval)duration{
    CATransition * stoneobs_temp_transion = [CATransition new];
    stoneobs_temp_transion.type = [self stringWithType:type];
    stoneobs_temp_transion.duration = duration;
    stoneobs_temp_transion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:stoneobs_temp_transion forKey:nil];
}

- (NSString*)stringWithType:(STAnimationType_stoneobs_enum)type{
    if (type == STAnimationTypekCATransitionPush_stoneobs_enum) {
        return @"push";
    }
    else if (type == STAnimationTypekCATransitionMoveIn_stoneobs_enum){
        return @"moveIn";
    }
    else if (type == STAnimationTypekCATransitionReveal_stoneobs_enum){
        return @"kCATransitionReveal";
    }
    else if (type == STAnimationTypekCATransitionFade_stoneobs_enum){
        return @"kCATransitionFade";
    }
    else if (type == STAnimationTypecube_stoneobs_enum){
        return @"cube";
    }
    else if (type == STAnimationTypesuckEffect_stoneobs_enum){
        return @"suckEffect";
    }
    else if (type == STAnimationTyperippleEffect_stoneobs_enum){
        return @"rippleEffect";
    }
    else if (type == STAnimationTypeSTpageCurle_stoneobs_enum){
        return @"pageCurl";
    }
    else if (type == STAnimationTypeSTpageUnCurl_stoneobs_enum){
        return @"pageUnCurl";
    }
    else if (type == STAnimationTypeoglFlip_stoneobs_enum){
        return @"oglFlip";
    }
    else if (type == STAnimationTypecameraIrisHollowOpen_stoneobs_enum){
        return @"CameraIrisHollowOpen";
    }
    else if (type == STAnimationTypecameraIrisHollowClose_stoneobs_enum){
        return @"CameraIrisHollowClose";
    }
    else if (type == STAnimationTypeCurlDown_stoneobs_enum){
        return @"CurlDown";
    }
    else if (type == STAnimationTypeCurlUp_stoneobs_enum){
        return @"CurlUp";
    }
    else if (type == STAnimationTypeFlipFromLeft_stoneobs_enum){
        return @"FlipFromLeft";
    }
    else if (type == STAnimationTypeFlipFromRight_stoneobs_enum){
        return @"FlipFromRight";
    }else{
    
        return @"STcameraIrisHollowClose";
    }
}
- (void)stoneobs_fun_st_showCircleRoundAnimationWithKey:(NSString*)key{
    CABasicAnimation *stoneobs_temp_rotationAnimation;
    
    stoneobs_temp_rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    stoneobs_temp_rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    
    stoneobs_temp_rotationAnimation.duration = 3;
    
    stoneobs_temp_rotationAnimation.repeatCount = HUGE_VALF;
    stoneobs_temp_rotationAnimation.removedOnCompletion = NO;
    
    if (key) {
        [self.layer addAnimation:stoneobs_temp_rotationAnimation forKey:key];
    }
    
    
}
#define ST_ACTIVITY_VIEW_TAG  10002
//展示菊花
- (void)stoneobs_fun_st_showActivityViewWithStyle:(UIActivityIndicatorViewStyle)style{
   [self stoneobs_fun_st_hideActivityView];
    UIActivityIndicatorView * stoneobs_temp_view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
    stoneobs_temp_view.frame = self.bounds;
    stoneobs_temp_view.tag = ST_ACTIVITY_VIEW_TAG;
    [stoneobs_temp_view startAnimating];
    [self addSubview:stoneobs_temp_view];
}
//隐藏菊花
- (void)stoneobs_fun_st_hideActivityView{
    [[self viewWithTag:ST_ACTIVITY_VIEW_TAG] removeFromSuperview];
}



#pragma mark --direction
- (UIImage *)stoneobs_fun_stoneobs_fun_st_snapshotImage {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *stoneobs_temp_snap = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return stoneobs_temp_snap;
}
- (UIImage *)stoneobs_fun_stoneobs_fun_st_snapshotImageAfterScreenUpdates:(BOOL)afterUpdates {
    if (![self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        return [self stoneobs_fun_stoneobs_fun_st_snapshotImage];
    }
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:afterUpdates];
    UIImage *stoneobs_temp_snap = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return stoneobs_temp_snap;
}

- (NSData *)stoneobs_fun_snapshotPDF {
    CGRect stoneobs_temp_bounds = self.bounds;
    NSMutableData *stoneobs_temp_data = [NSMutableData data];
    CGDataConsumerRef stoneobs_temp_consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)stoneobs_temp_data);
    CGContextRef stoneobs_temp_context = CGPDFContextCreate(stoneobs_temp_consumer, &stoneobs_temp_bounds, NULL);
    CGDataConsumerRelease(stoneobs_temp_consumer);
    if (!stoneobs_temp_context) return nil;
    CGPDFContextBeginPage(stoneobs_temp_context, NULL);
    CGContextTranslateCTM(stoneobs_temp_context, 0, stoneobs_temp_bounds.size.height);
    CGContextScaleCTM(stoneobs_temp_context, 1.0, -1.0);
    [self.layer renderInContext:stoneobs_temp_context];
    CGPDFContextEndPage(stoneobs_temp_context);
    CGPDFContextClose(stoneobs_temp_context);
    CGContextRelease(stoneobs_temp_context);
    return stoneobs_temp_data;
}

- (void)setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius {
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = offset;
    self.layer.shadowRadius = radius;
    self.layer.shadowOpacity = 1;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

- (void)removeAllSubviews {
    //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    while (self.subviews.count) {
        [self.subviews.lastObject removeFromSuperview];
    }
}


- (UIViewController *)viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
//有前缀
- (CGFloat)st_left {
    return self.frame.origin.x;
}

- (void)setSt_left:(CGFloat)left {
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

- (CGFloat)st_top {
    return self.frame.origin.y;
}

- (void)setSt_top:(CGFloat)top {
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (CGFloat)st_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setSt_right:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)st_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setSt_bottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)st_width {
    return self.frame.size.width;
}

- (void)setSt_width:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)st_height {
    return self.frame.size.height;
}

- (void)setSt_height:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGPoint)st_origin {
    return self.frame.origin;
}

- (void)setSt_origin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)st_size {
    return self.frame.size;
}

- (void)setSt_size:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
- (void)setSt_centerX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)st_centerX
{
    return self.center.x;
}

- (void)setSt_centerY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)st_centerY
{
    return self.center.y;
}

#pragma mark --shadow
- (void)stoneobs_fun_stoneobs_fun_st_showTopShadow
{
    self.layer.shadowOffset = CGSizeMake(0, -0.8);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_stoneobs_fun_st_showBottomShadow
{
    self.layer.shadowOffset = CGSizeMake(0, 0.8);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_stoneobs_fun_st_showLeftShadow
{
    self.layer.shadowOffset = CGSizeMake(-1, 0);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_stoneobs_fun_st_showRightShadow
{
    self.layer.shadowOffset = CGSizeMake(1, 0);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = .5f;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
- (void)stoneobs_fun_stoneobs_fun_st_showRightAndBottomShadow
{
    self.layer.shadowOffset = CGSizeMake(1.5, 1.5);
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowRadius = 1;
    self.layer.shadowOpacity = 1.0;
    CGRect shadowFrame = self.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath
                            bezierPathWithRect:shadowFrame].CGPath;
    self.layer.shadowPath = shadowPath;
}
//展示周围的layer 到父view
- (CALayer*)stoneobs_fun_stoneobs_fun_st_showRoundLayer{
    CALayer *layer = [CALayer layer];
    layer.frame = self.frame;
    layer.backgroundColor = [UIColor clearColor].CGColor;
    layer.shadowColor =  [UIColor.grayColor colorWithAlphaComponent:0.3].CGColor;
    layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width + 5, self.frame.size.height + 5) cornerRadius:self.layer.cornerRadius].CGPath;
    layer.shadowOpacity = 0.5;
    layer.shadowOffset = CGSizeMake(-3, -2);
    layer.cornerRadius = self.layer.cornerRadius;
    //这里self表示当前自定义的view
    [self.superview.layer insertSublayer:layer below:self.layer];
    return layer;
}
- (CALayer*)stoneobs_fun_stoneobs_fun_st_showRoundLayerWithColor:(UIColor*)color{
    CALayer *layer = [CALayer layer];
    layer.frame = self.frame;
    layer.backgroundColor = [UIColor clearColor].CGColor;
    layer.shadowColor =  color.CGColor;
    layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width + 5, self.frame.size.height + 5) cornerRadius:self.layer.cornerRadius].CGPath;
    layer.shadowOpacity = 0.5;
    layer.shadowOffset = CGSizeMake(-3, -2);
    layer.cornerRadius = self.layer.cornerRadius;
    //这里self表示当前自定义的view
    [self.superview.layer insertSublayer:layer below:self.layer];
    return layer;
}
//展示左右上下线
- (void)stoneobs_fun_stoneobs_fun_st_showRightLine:(CGFloat)height
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-1, 1, 0.8, self.frame.size.height-2)];
    line.backgroundColor = ST_lineColor;
    [self addSubview:line];
    if (height) {
        CGRect frame = line.frame;
        frame.size.height = height;
        line.frame = frame;
        line.center = CGPointMake(line.center.x, self.frame.size.height/2);
    }
}
- (void)stoneobs_fun_stoneobs_fun_st_showRightInsetLine:(CGFloat)height insetx:(CGFloat)insetx{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-1, 1, 0.8, self.frame.size.height-insetx)];
    line.backgroundColor = ST_lineColor;
    [self addSubview:line];
    if (height) {
        CGRect frame = line.frame;
        frame.size.height = height;
        line.frame = frame;
        line.center = CGPointMake(line.center.x, self.frame.size.height/2);
    }
}
- (void)stoneobs_fun_stoneobs_fun_st_showLeftLine:(CGFloat)height
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 0.8, self.frame.size.height - 2)];
    line.backgroundColor = ST_lineColor;
    [self addSubview:line];
    if (height) {
        CGRect frame = line.frame;
        frame.size.height = height;
        line.frame = frame;
        line.center = CGPointMake(line.center.x, height/2);
    }
}

- (void)stoneobs_fun_stoneobs_fun_st_showBottomLine
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width , 0.5)];
    line.backgroundColor = ST_lineColor;
    [self addSubview:line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * scroll = (UIScrollView*)self;
        line.frame = CGRectMake(0, self.frame.size.height-1, scroll.contentSize.width , 1);
    }
}
- (void)stoneobs_fun_stoneobs_fun_st_showBottomLineAndWitdh:(CGFloat)witdh{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, witdh , 0.5)];
    line.backgroundColor = ST_lineColor;
    CGPoint center = self.center;
    center.x = self.frame.size.width / 2;
    center.y = self.frame.size.height - 0.5;
    line.center = center;
    [self addSubview:line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * scroll = (UIScrollView*)self;
        line.frame = CGRectMake(0, self.frame.size.height-1, scroll.contentSize.width , 1);
    }
}
- (void)stoneobs_fun_stoneobs_fun_st_showTopLine
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , 0.5)];
    line.backgroundColor = ST_lineColor;
    [self addSubview:line];
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView * scroll = (UIScrollView*)self;
        line.frame = CGRectMake(1, self.frame.size.height-1, scroll.contentSize.width - 2, 1);
    }
}
//边框 一般0.3 黑色
- (void)stoneobs_fun_stoneobs_fun_st_setBrderWidth:(CGFloat)width borderColor:(UIColor*)borderColor
{
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = width;
}
//展示虚线边框
- (void)stoneobs_fun_stoneobs_fun_st_setDottedLineBrderWidth:(CGFloat)width
                       borderColor:(UIColor*)borderColor{
    
    for (CALayer *layer in self.layer.sublayers) {
        if ([layer isKindOfClass:CAShapeLayer.class]) {
            [layer removeFromSuperlayer];
        }
    }
    
    CAShapeLayer *border = [CAShapeLayer layer];
    //虚线的颜色
    border.strokeColor = borderColor.CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    //设置路径
    border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    border.frame = self.bounds;
    //虚线的宽度
    border.lineWidth = width;
    //设置线条的样式 //
    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@4, @2];
    [self.layer addSublayer:border];

}
- (void)stoneobs_fun_stoneobs_fun_st_setBorderWith:(CGFloat)width borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)radius{
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = width;
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}
@end
